/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2019年1月7日
 */

package com.massyframework.beanskin.spring.beancontext;


import java.lang.annotation.Annotation;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.massyframework.beanskin.assembly.Assembly;
import com.massyframework.beanskin.assembly.AssemblyContext;
import com.massyframework.beanskin.assembly.beans.BeanNotFoundException;
import com.massyframework.beanskin.assembly.handling.LifecycleManager;

/**
 * {@link SpringAssemblyContext}, 封装{@link SpringBeanContextAdapter}形成装配件上下文
 */
public final class SpringAssemblyContext implements AssemblyContext {

    private final LifecycleManager lifecycleManager;
    private final SpringBeanContextAdapter beanContext;

    public SpringAssemblyContext(LifecycleManager lifecycleManager, SpringBeanContextAdapter beanContext) {
        this.lifecycleManager = Objects.requireNonNull(
                lifecycleManager, "\"lifecycleManager\" cannot be null.");
        this.beanContext = Objects.requireNonNull(beanContext, "\"beanContext\" cannot be null.");
    }

    /**
     * 关联的装配件
     *
     * @return {@link Assembly}
     */
    @Override
    public Assembly getAssociatedAssembly() {
        return this.lifecycleManager.getAssociatedAssembly();
    }

    /**
     * 判断<code>beanName</code>的Bean是否存在
     *
     * @param beanName {@link String}, bean名称
     * @return {@link boolean}, 返回<code>true</code>表示存在，返回<code>false</code>表示不存在。
     */
    @Override
    public boolean containsBean(String beanName) {
        return this.beanContext.containsBean(beanName);
    }

    /**
     * 根据<code>beanName</code>获取Bean实例
     *
     * @param beanName {@link String}, bean名称
     * @return {@link Object}
     * @throws BeanNotFoundException 未找到Bean时抛出例外
     */
    @Override
    public Object getBean(String beanName) throws BeanNotFoundException {
        return this.beanContext.getBean(beanName);
    }
    
    

    @Override
	public Class<?> getBeanType(String beanName) throws BeanNotFoundException {
		return this.beanContext.getBeanType(beanName);
	}

	/**
     * 获取所有派生于<code>beanType</code>的实例<br>
     *
     * @param beanType {@link Class}, bean实现的类型
     * @return {@link Map}
     */
    @Override
    public <B> Map<String, B> getBeansOfType(Class<B> beanType) {
        return this.beanContext.getBeansOfType(beanType);
    }

    /**
     * 获取所有在类上注解<code>annoType</code>的bean实例
     *
     * @param annoType {@link Class}.注解类型。
     * @return {@link Map}
     */
    @Override
    public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annoType) {
        return this.beanContext.getBeansWithAnnotation(annoType);
    }
    
    @Override
	public List<String> getBeanNames() {
		return this.beanContext.getBeanNames();
	}

	/**
     * Bean上下文
     * @return {@link SpringBeanContextAdapter}
     */
    public SpringBeanContextAdapter getBeanContext(){
        return this.beanContext;
    }

    @Override
    public String toString() {
        return "SpringAssemblyContext";
    }
}
